// 用于自动保存草稿的模块
// todo 需要依赖editData对象 main内容还未更改
define( [
  'require',
  'jquery',
  'bigwe_utils',
  '../lib/editData',
  'message',
  'constants',
  '../lib/utils'
], function( require, $, bu ) {

  var ShowTitleMessage = require( 'message' );
  var showTime = ShowTitleMessage.showTime;
  var c = require( 'constants' );
  return {
    options: {
      // 是否自动保存草稿
      autosave: true,
      // 自动保存间隔时间
      saveInterval: 1e3,
      // 缓存键的开头(尽量不被覆盖)
      storageKey: 'bigwe',
      // 自动保存的ID
      autosaveID: ''
    },
    _getStorageKey: function() {
      var storageKey = 'bigwe' + $( '#user-id-bg', document ).val();
      var editType = bu.getUrlParam( 'edit_type' );
      var articleID = bu.getUrlParam( 'cd' );
      var articleGroup = bu.getUrlParam( 'article_group' );
      if ( editType == '1' ) {
        storageKey += 'article';
        if ( articleID ) {
          storageKey += articleID;
        }
      } else {
        storageKey += 'graph';
        if ( articleGroup ) {
          storageKey += articleGroup;
        }
      }
      return bu.md5( storageKey );
    },
    // 自动保存
    _autosaveLocalStorage: function() {
      var editData = require( '../lib/editData' );
      var jsonStr = JSON.stringify( editData.graphDataDetails );
      var storageKey = 'bigwe' + c.userID;
      var editType = bu.getUrlParam( 'edit_type' );
      var articleID = bu.getUrlParam( 'cd' );
      var articleGroup = bu.getUrlParam( 'article_group' );

      if ( editType == '1' ) {
        storageKey += 'article';
        if ( articleID ) {
          storageKey += articleID;
        }
      } else {
        storageKey += 'graph';
        if ( articleGroup ) {
          storageKey += articleGroup;
        }
      }
      window.localStorage.setItem( bu.md5( storageKey ), jsonStr );
    },
    // 获取本地缓存
    getLocalStorage: function() {
      return window.localStorage.getItem( this._getStorageKey() );
    },
    // 填充数据到页面
    fullDataToPage: function() {
      var editData = require( '../lib/editData' );
      var jsonObj = $.parseJSON( this.getLocalStorage() || '[]' );
      var isNullData = true;
      jsonObj.forEach( function( item ) {
        for ( var key in item ) {
          // 此处的数组用于排除要check的字段
          if ( isNullData && $.inArray( key, [ 'isAllCheck', 'isEdit', 'show_cover_pic' ] ) === -1 && item[ key ] !== '' && item[ key ] != null && item[ key ] != 0 ) {
            isNullData = false;
          }
        }
      } );

      if ( isNullData ) {
        ShowTitleMessage.showWarning( '没有草稿或已清除', showTime );
        return;
      }
      // 重置当前图文的数据
      editData.allIsReady = 2;
      require( '../lib/utils' ).graghListCreateJsons( jsonObj );
    },
    // 清空本地缓存
    clear: function() {
      var editData = require( '../lib/editData' );
      this.stop();
      window.localStorage.removeItem( this._getStorageKey() );
      editData.isContentChange = false;
    },
    // 判断是否有草稿再显示草稿按钮
    showDraftLink: function() {
      if ( $.trim( this.getLocalStorage() ) ) {
        //$( '.draft-text', $( '.autosave' ).show() ).css( 'display', 'inline-block' );
        $( '.js_draft_fullin' ).removeClass( 'disabled' );
      } else {
        $( '.js_draft_fullin' ).addClass( 'disabled' ).attr( 'data-original-title', '暂无草稿' );
        //$( '.draft-text', '.autosave' ).hide();
      }
    },
    start: function() {
      if ( this.options.autosave ) {
        if ( !this.options.autosaveID ) {
          //console.log('autosave start')
          //$( '.autosave-text', $( '.autosave' ).show() ).css( 'display', 'inline-block' );
          showSuccessMessage( '自动保存已开启' );
          this.options.autosaveID = setInterval( this._autosaveLocalStorage, this.options.saveInterval );
        }
      }
    },
    stop: function() {
      clearInterval( this.options.autosaveID );
      this.options.autosaveID = '';
    }
  };
} );